let map = {}
let emit = function(name, ...data) {
	let e = map[name]
	//如果有回调，直接触发回调
	if (e && e.callback) {
		e.callback(...data)
		if (e.type == 'once') {
			delete map[name]
		}
	} else {
		//否则添加待响应事件
		map[name] = {
			data
		}
	}
}

let once = function(name, callback) {
	let e = map[name]
	//如果已有e，触发回调
	if (e) {
		callback(e.data)
		delete map[name]
	} else {
		//没有e，添加回调
		map[name] = {
			callback,
			type:'once'
		}
	}
}
/**
 * 和once一样，只是不delete
 */
let on = function(name, callback) {
	let e = map[name]
	if (e) {
		callback(e.data)
	} else {
		map[name] = {
			callback
		}
	}
}

let del=function(name){
	let e= map[name]
	if(e){
		delete map[name]
		return true
	}else{
		return false
	}
}

let has=function(name){
	let e= map[name]
	if(e){
		return true
	}else{
		return false
	}
}

export default {
	install(vue) {
		vue.prototype.$call = {
			once,
			emit,
			on,
			del,
			has
		}
	}
}
